package day9;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author oldliu
 * @since 1.0
 */
public class TalkServer {
    public static void main(String[] args) {

        try {
            ServerSocket st = new ServerSocket(9999);
            System.out.println("listen on 9999端口");
            while (true) {
                Socket s = st.accept();
                System.out.println(s + "链接了。。。");
                System.out.println(s.getInetAddress());
                System.out.println(s.getPort());
                System.out.println(s.getLocalPort());
                try {
                    InputStream in = s.getInputStream();
                    OutputStream out = s.getOutputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(in));
                    PrintWriter pw = new PrintWriter(out, true);
                    String msg = null;
                    while ((msg = br.readLine()) != null) {
                        System.out.println("客户端说:" + msg);
                        pw.println("你说的：" + msg + " 朕知道了。。。");
                        if (msg.equals("exit"))
                            break;
                    }
                    s.close();
                }catch (Exception e){
                    e.printStackTrace();
                    System.out.println(s+"意外退出了。。");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
